<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible"
    >
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
            <el-form-item label="项目名称" prop="roadworkContent"
                :rules="[
                 ]">
              <el-input v-model="inputForm.roadworkContent" placeholder="请填写项目名称"     disabled></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="提出人" prop="unsuccessfulPeople"
                :rules="[
                 ]">
              <el-input v-model="inputForm.unsuccessfulPeople" placeholder="请填写提出人"  disabled   ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="工时" prop="workTime"
                :rules="[
                 ]">
              <el-input v-model="inputForm.workTime" placeholder="请填写工时"    disabled="" ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="状态" prop="state"
                :rules="[
                 ]">
              <el-select v-model="inputForm.state" placeholder="请填状态" style="width:100%;" disabled="">
              <el-option v-for="state in $dictUtils.getDictList('repair_statement')" :label="state.label" :value="state.value" :key="state.value"></el-option>
              </el-select>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="项目协办人" prop="projectMan"
                :rules="[
                 ]">
              <el-input v-model="inputForm.projectMan" placeholder="请填写项目协办人"     disabled=""></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
                    <el-form-item label="检修标准工时" prop="overhaulPeople"
                        :rules="[
                         ]">
                      <el-input v-model="inputForm.overhaulPeople" placeholder="请填写检修标准工时" disabled=""></el-input>
                   </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="检修标准项目承办人数" prop="repirpeople"
                        :rules="[
                         ]">
                      <el-input v-model="inputForm.repirpeople" placeholder="请填写检修标准项目承办人数" disabled=""></el-input>
                   </el-form-item>
                </el-col>
        <el-col :span="12">
            <el-form-item label="工号" prop="workNumber"
                :rules="[
                 ]">
              <el-input v-model="inputForm.workNumber" placeholder="请填写工号"  disabled   ></el-input>
           </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
            <el-form-item label="备注信息" prop="remarks"
                :rules="[
                 ]">
          <el-input type="textarea" v-model="inputForm.remarks" placeholder="请填写备注信息"     ></el-input>
           </el-form-item>
        </el-col> -->
    <el-col :span="24">
     <el-form-item label-width="0">
            <el-tabs v-model="repairPlanTab">
            <el-tab-pane label="材料准备附表">
                  <el-button size="small" @click="addRepairPlanPttachRow" type="primary">新增</el-button>
                  <el-table
                  border
                  :data="inputForm.repairPlanPttachList.filter(function(item){ return item.delFlag !== '1'})"
                  style="width: 100%">
                <el-table-column
                    prop="repairType"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="规格型号">
                  </el-table-column>
                <el-table-column
                    prop="repairUnit"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="单位">
                  </el-table-column>
                <el-table-column
                    prop="repairAmount"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="数量">
                  </el-table-column>
                <el-table-column
                    prop="repairSymbol"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="单价">
                  </el-table-column>
                <el-table-column
                    prop="repairSum"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="金额">
                  </el-table-column>
                <el-table-column
                    prop="repairTime"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="要求使用时间">
                  </el-table-column>
                <el-table-column
                    prop="remarks"
                    header-align="center"
                    align="center"
                    show-overflow-tooltip
                    label="备注信息">
                  </el-table-column>
                  <el-table-column
                    fixed="right"
                    label="操作"
                    width="150">
                    <template slot-scope="scope">
                      <el-button @click="viewRepairPlanPttachRow(scope.row)" type="text" size="small">查看</el-button>
                      <el-button  @click="editRepairPlanPttachRow(scope.row)" type="text" size="small">编辑</el-button>
                      <el-button  @click="delRepairPlanPttachRow(scope.row)" type="text" size="small">删除</el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
           </el-form-item>
          </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button :disabled="method === 'changestate'" @click="visible = false">关闭</el-button>
      <el-button :disabled="method === 'changestate'" type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
    </el-dialog>
  <RepairPlanPttachForm ref="repairPlanPttachForm"  @addRow="saveRepairPlanPttachRow(arguments)"></RepairPlanPttachForm>
</div>
</template>

<script>
  import RepairPlanPttachForm from './RepairPlanPttachForm'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        repairPlanTab: '0',
        inputForm: {
          id: '',
          repairPlanPttachList: [],
          roadworkContent: '',
          unsuccessfulPeople: '',
          workNumber: '',
          workTime: '',
          state: '',
          projectMan: '',
          remarks: '',
          testId: '',
          checkTime: '',
          checkPeopleNumber: '',
          repirpeople: '',
          overhaulPeople: '',
          processId: ''
        }
      }
    },
    components: {
      RepairPlanPttachForm
    },
    methods: {
      init (method, id) {
        this.method = method
        this.$http({
           url: `/sys/log/logId`,
           method: 'get'
         }).then(({data}) => {
          this.loginName = data.logId.roleNames
          // if (this.loginName !== '点检员' && method !== 'view') {
          //   this.$alert('您没有此权限', '提示', {
          //     confirmButtonText: '确定',
          //     type: 'warning'
          //   })
          // } else {
        if (method === 'add') {
          this.title = `新建材料准备`
        } else if (method === 'edit') {
          this.title = '修改材料准备'
        } else if (method === 'view') {
          this.title = '查看材料准备'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.inputForm.id = id
          this.repairPlanTab = '0'
          this.inputForm.repairPlanPttachList = []
          if (method === 'edit' || method === 'view' || method === 'changestate') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/flowform/form/repairPlan/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.repairPlan)
              this.loading = false
              if (method === 'changestate') {
                this.inputForm.state = 1
                this.doSubmit()
              }
            })
          }
        })
        // }
        })
      },
      saveRepairPlanPttachRow (child) {
        if (child[0] === '') {
          this.inputForm.repairPlanPttachList.push(child[1])
        } else {
          this.inputForm.repairPlanPttachList.forEach((item, index) => {
            if (item === child[0]) {
              this.inputForm.repairPlanPttachList.splice(index, 1, child[1])
            }
          })
        }
      },
      addRepairPlanPttachRow (child) {
        this.$refs.repairPlanPttachForm.init('add')
      },
      viewRepairPlanPttachRow (child) {
        this.$refs.repairPlanPttachForm.init('view', child)
      },
      editRepairPlanPttachRow (child) {
        this.$refs.repairPlanPttachForm.init('edit', child)
      },
      delRepairPlanPttachRow (child) {
        this.inputForm.repairPlanPttachList.forEach((item, index) => {
          if (item === child && item.id === '') {
            this.inputForm.repairPlanPttachList.splice(index, 1)
          } else if (item === child) {
            item.delFlag = '1'
            this.inputForm.repairPlanPttachList.splice(index, 1, item)
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/flowform/form/repairPlan/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
